//
//  FriendViewController.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/22.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "FriendViewController.h"
#import "UserNameTableViewCell.h"

@interface FriendViewController ()<EMChatManagerDelegate>

@property (nonatomic, retain) UserNameTableViewCell *userCell;

@property (nonatomic, retain) UIButton *addButton;

@end

@implementation FriendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.userCell = [[UserNameTableViewCell alloc] init];
    [self.view addSubview:_userCell];
    
    self.addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_addButton];
    
    [self createSubViews];
    
    // Do any additional setup after loading the view.
}

-(void)createSubViews {
    
    [self.userCell mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.view).with.offset(20);
        make.left.equalTo(self.view).with.offset(0);
        make.width.offset(WIDTH);
        make.width.offset(HEIGHT / 12);
        
    }];
    
    self.userCell.userName = self.userName;
    
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.view).with.offset(150);
        make.left.equalTo(self.view).with.offset(40);
        make.width.offset(WIDTH - 80);
        make.height.offset(30);
        
    }];
    
    [self.addButton setTitle:@"打招呼" forState:UIControlStateNormal];
    self.addButton.backgroundColor = [UIColor redColor];
    self.addButton.layer.cornerRadius = 8;
    self.addButton.layer.masksToBounds = YES;
    [self.addButton addTarget:self action:@selector(addFriend:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)addFriend:(UIButton *)button {
    
    NSString *username = [[EaseMob sharedInstance].chatManager loginInfo][@"username"];
    
    NSString *text = [NSString stringWithFormat:@"我是%@", username];
    BOOL result = [[EaseMob sharedInstance].chatManager addBuddy:self.userName message:text error:nil];
    if (result) {
        UIAlertController *alertD = [UIAlertController alertControllerWithTitle:@"好友申请成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *aaction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertD addAction:aaction];
        [self presentViewController:alertD animated:YES completion:^{
            
        }];
        
    }else{
        NSLog(@"添加失败");
        
    }
    
}

//协议方法 通讯录改变时调用
-(void)didUpdateBuddyList:(NSArray *)buddyList changedBuddies:(NSArray *)changedBuddies isAdd:(BOOL)isAdd{
    
    
}
//被好友删除时调用
-(void)didRemovedByBuddy:(NSString *)username{
    NSLog(@"我被%@删除了", username);
}

-(void)setUserName:(NSString *)userName {
    
    if (_userName != userName) {
        _userName = userName;
    }
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
